axios.defaults.baseURL = 'http://api-breakingnews-web.itheima.net';

axios.interceptors.request.use(function (config) {
    if (config.url.indexOf('/my') !== -1) {
        config.headers.Authorization = localStorage.getItem('token')
    }
    return config;
}, function (error) {
    return Promise.reject(error)
})

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    if (response.data.message === "身份认证失败！") {
        //不让他跳转到Index界面, 让他回到登录界面
        location.href = '/login.html'
        //如果认证失败,消除本地数据
        localStorage.removeItem('token')
    }

    return response
}, function (error) {
    return Promise.reject(error)
})